package com.hitown.controller.dxhd;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitown.model.dxhd.DxhdAbry;
import com.hitown.model.dxhd.DxhdRybs;
import com.hitown.service.dxhd.DxhdRybsService;
import com.hitown.util.Params;
import com.hitown.util.R;
import com.hitown.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 人员背审信息表 前端控制器
 * </p>
 *
 * @author menu
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/dxhd-rybs")
public class DxhdRybsController {

    @Autowired
    private DxhdRybsService dxhdRybsService;

    @ApiOperation(value = "分页查询")
    @RequestMapping("selectPage")
    public R selectPage(@ApiIgnore @RequestBody Params<DxhdRybs> params){
        Page<DxhdRybs> page = new Page<>(params.getCurrent(),params.getSize());
        QueryWrapper<DxhdRybs> wrapper =new QueryWrapper<>();
        //姓名
        if(!StringUtils.isEmpty(params.getEntity().getXm())){
            wrapper.like("XM",params.getEntity().getXm());
        }
        //身份证
        if(!StringUtils.isEmpty(params.getEntity().getZjhm())){
            wrapper.like("ZJHM",params.getEntity().getZjhm());
        }

        //性别
        if(!StringUtils.isEmpty(params.getEntity().getXbdm())){
            wrapper.eq("XBDM",params.getEntity().getXbdm());
        }
        //审查时间
        wrapper.orderByDesc("SCSJ");
        IPage<DxhdRybs> pageVo = dxhdRybsService.page(page,wrapper);
        for (DxhdRybs e: pageVo.getRecords()) {
            if(e.getZp()!=null){
                String base64Str = "data:image/jpg;base64,"+DatatypeConverter.printBase64Binary(e.getZp());
                e.setZpDz(base64Str);
            }
        }
        return R.ok().put("data",pageVo);

    }


    @ApiOperation(value = "根据id查询记录")
    @RequestMapping("getById")
    public R getById(@RequestBody DxhdRybs entit){
        DxhdRybs result = dxhdRybsService.getById(entit.getZjhm());
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "保存单条记录")
    @PostMapping("save")
    public R save(@RequestBody DxhdRybs entit){
        String ajbh =entit.getAjbh();
        if(ajbh==null || ajbh==""){
            entit.setAjbh(UUID.randomUUID().toString());
        }
        Boolean result = dxhdRybsService.save(entit);
        return R.ok().put("data",result);
    }


    @ApiOperation(value = "批量保存")
    @RequestMapping("saveBatch")
    public R saveBatch(@RequestBody Collection<DxhdRybs> entityList){
        Boolean result = dxhdRybsService.saveBatch(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("updateById")
    public R updateById(@RequestBody DxhdRybs entit){
        entit.setZp(null);
        entit.setZpDz(null);
        Boolean result = dxhdRybsService.updateById(entit);
        return R.ok().put("data",result);
    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("update")
    public R update(@RequestParam String ajbh,@RequestParam("file") MultipartFile file) throws IOException {
        DxhdRybs entity = new DxhdRybs();
        entity.setZp(file.getBytes());
        if(!StringUtils.isEmpty(ajbh) && !ajbh.equals("undefined")){
            entity.setAjbh(ajbh);
            dxhdRybsService.updateById(entity);
        }else{
            entity.setAjbh(UUID.randomUUID().toString());
            dxhdRybsService.save(entity);
        }
        return R.ok().put("data",entity.getAjbh());
    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("handleRemove")
    public R handleRemove(@RequestBody DxhdRybs entity) throws IOException {
      //  DxhdRybs entity = new DxhdRybs();
        entity.setZp(new byte[0]);
      //  entity.setAjbh(ajbh);
        dxhdRybsService.updateById(entity);
        return R.ok().put("data",entity.getAjbh());
    }

    @ApiOperation(value = "根据ID 批量更新")
    @RequestMapping("updateBatchById")
    public R updateBatchById(@RequestBody Collection<DxhdRybs> entityList){
        Boolean result = dxhdRybsService.updateBatchById(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 删除")
    @RequestMapping("removeById")
    public R removeById(@RequestBody DxhdRybs entity){
        Boolean result = dxhdRybsService.removeById(entity.getAjbh());
        return R.ok().put("data",result);

    }



    @ApiOperation(value = "删除（根据ID 批量删除）")
    @RequestMapping("removeByIds")
    public R removeByIds(@RequestBody Collection<String> Ids){
        Boolean result = dxhdRybsService.removeByIds(Ids);
        return R.ok().put("data",result);
    }
}

